package com.manor.main.activity;

import android.view.View;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.manor.common.activity.AbsActivity;
import com.manor.common.http.HttpCallback;
import com.manor.common.utils.ClickUtil;
import com.manor.common.utils.StringUtil;
import com.manor.common.utils.ToastUtil;
import com.manor.dynamic.activity.DynamicDetailsActivity;
import com.manor.dynamic.bean.CommunityDynamicBean;
import com.manor.dynamic.bean.DynamicBean;
import com.manor.main.R;
import com.manor.main.adapter.CommentsAndLikesAdapter;
import com.manor.main.bean.CommentsAndLikesBean;
import com.manor.main.http.MainHttpUtil;

/**
 * 评论点赞列表
 */
public class CommentsAndLikesActivity extends AbsActivity {

    private SmartRefreshLayout mSrlCommentsLikes;
    private RecyclerView mRvCommentsLisks;
    CommentsAndLikesAdapter commentsAndLikesAdapter;
    private List<CommentsAndLikesBean> commentsAndLikesBeanList = new ArrayList<>();
    private DynamicBean dynamicBean;
    int p = 1;
    public static final String type = "1";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_comments_and_likes;
    }

    @Override
    protected void main() {
        super.main();
        setTitle("评论 & 赞");
        initView();
        initDate();
        mSrlCommentsLikes.autoRefresh();
    }

    private void getDate(int p) {
        MainHttpUtil.getDynamicLog(p, new HttpCallback() {
            @Override
            public void onSuccess(int code, String msg, String info) {
                if (code == 0) {
                    if (StringUtil.isNotEmpty(info)) {
                        if (p > 1) {
                            List<CommentsAndLikesBean> commentsAndLikesBeans = JSON.parseArray(info, CommentsAndLikesBean.class);
                            commentsAndLikesBeanList.addAll(commentsAndLikesBeans);
                        } else {
                            if (commentsAndLikesBeanList.size() > 0) {
                                commentsAndLikesBeanList.clear();
                            }
                            List<CommentsAndLikesBean> commentsAndLikesBeans = JSON.parseArray(info, CommentsAndLikesBean.class);
                            commentsAndLikesBeanList.addAll(commentsAndLikesBeans);
                        }
                    } else {
                        mSrlCommentsLikes.finishLoadMoreWithNoMoreData();
                    }

                    commentsAndLikesAdapter.notifyDataSetChanged();

                } else {
                    ToastUtil.show(msg);
                }
                mSrlCommentsLikes.finishRefresh();
                mSrlCommentsLikes.finishLoadMore();
            }

            @Override
            public void onError() {
                super.onError();
                mSrlCommentsLikes.finishRefresh();
                mSrlCommentsLikes.finishLoadMore();
            }
        });
    }

    private void initDate() {
        mSrlCommentsLikes.setHeaderTriggerRate(0.1f);
        mSrlCommentsLikes.setHeaderMaxDragRate(1f);
        mSrlCommentsLikes.setHeaderHeight(60f);
        mSrlCommentsLikes.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                p += 1;
                getDate(p);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                p = 1;
                getDate(p);
            }
        });

        commentsAndLikesAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (ClickUtil.canClick()) {

                    if ("1".equals(commentsAndLikesBeanList.get(position).getType())) {
                        String toUid = commentsAndLikesBeanList.get(position).getUid();//跳转的用户的ID
                        String dynamicUid = commentsAndLikesBeanList.get(position).getA_id();//视频ID
                        String comment_id = commentsAndLikesBeanList.get(position).getComment_id();//评论ID

                        UserVideoDetailActivity.Companion.forward(CommentsAndLikesActivity.this,toUid,dynamicUid,comment_id);

                    } else {
                         CommunityDynamicBean dynamicBean = commentsAndLikesBeanList.get(position).getData();
                    DynamicDetailsActivity.Companion.forward(CommentsAndLikesActivity.this, dynamicBean);
                    }
                }
            }
        });

    }

    private void initView() {

        mSrlCommentsLikes = findViewById(R.id.srl_comments_likes);
        mRvCommentsLisks = findViewById(R.id.rv_comments_lisks);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRvCommentsLisks.setLayoutManager(linearLayoutManager);
        commentsAndLikesAdapter = new CommentsAndLikesAdapter(R.layout.rv_comments_lisks_item, commentsAndLikesBeanList);
        mRvCommentsLisks.setAdapter(commentsAndLikesAdapter);
    }
}
